package com.xxx.video.guide.views;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.configs.Constant;
import com.xxx.video.commen.utils.DownloadUtils;
import com.xxx.video.commen.utils.InstallOrLaunchUtils;
import com.xxx.video.commen.utils.RecyclerviewDownloadListener;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.views.BaseDialog;

import java.io.File;

import butterknife.BindView;

/**
 *
 *
 *
 */
public class DownloadAppProgressDialog extends BaseDialog {

    @BindView(R.id.pb_dialog_app_download_progress)
    ProgressBar pbDialogAppDownloadProgress;
    @BindView(R.id.tv_dialog_app_download_progress)
    TextView tvDialogAppDownloadProgress;
    @BindView(R.id.tv_dialog_app_download_progress_tip)
    TextView tvDialogAppDownloadProgressTip;
    private String downloadUrl;

    public DownloadAppProgressDialog(@NonNull Context context, String downloadUrl) {
        super(context);
        this.downloadUrl = downloadUrl;
        if (!TextUtils.isEmpty(downloadUrl)) {
            downloadApp();
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.dialog_app_download_progress;
    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(downloadUrl)) {
            downloadApp();
        }
    }

    /**
     * download app
     */
    private void downloadApp() {
        if (tvDialogAppDownloadProgressTip == null) {
            return;
        }
        //"/200808/ab2445bf-2c10-47c4-94f4-f42147859e4c.apk"
        downloadUrl = ResourcesUtils.getInstance().getDownloadPath(downloadUrl);
        BaseViewHolder baseViewHolder = new BaseViewHolder(tvDialogAppDownloadProgressTip) {
            @Override
            public void setData2View(int position) {

            }

            @Override
            public void refresh(int progress) {
                super.refresh(progress);
                if (pbDialogAppDownloadProgress != null) {
                    pbDialogAppDownloadProgress.setProgress(progress);
                    tvDialogAppDownloadProgress.setText(progress + "%");
                }
                if (progress == 100) {
                    /*
                     * download finish
                     */
                    dismiss();
                    InstallOrLaunchUtils.getInstance().installApk(context, new File(Constant.DOWNLOAD_VIDEO_PATH + DownloadUtils.getInstance().getFileName(downloadUrl)));
                }
            }
        };
        baseViewHolder.setTag(downloadUrl);
        DownloadUtils.getInstance().download(downloadUrl, new RecyclerviewDownloadListener(baseViewHolder));
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView(View view) {

    }
}
